from Pyro.protocol import DefaultConnValidator
import hmac
import hashlib


class UserLoginConnValidator(DefaultConnValidator):    
    
    def createAuthToken(self, authid, challenge, peeraddr, URI, daemon):
        # authid is what mungeIdent returned, a tuple (login, hash-of-password)
        # we return a secure auth token based on the server challenge string.
        return "%s:%s" % (authid[0], hmac.new(challenge,authid[1]).digest())
    
    def mungeIdent(self, ident):
        # ident is tuple (login, password), the client sets this.
        # we don't like to store plaintext passwords so store the md5 hash instead.
        return (ident[0], hashlib.md5(ident[1]).digest()) #@UndefinedVariable
    
